package test08.ts02;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author czl
 * @Date 2024-04-15
 */
public class MainTest03 {
    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 3, 2, 4, 6, 8, 9);


        // 1.最大值 - 同 max 和 min 一样，必须是升序
        Optional<Integer> maxOptional1 = list.stream()
                .collect(Collectors.maxBy((o1, o2) -> o1 - o2));
        System.out.println("最大值："+maxOptional1.get());

        // Stream流的max方法也可以实现
        Optional<Integer> collect1 = list.stream().max((o1, o2) -> o1 - o2);
        System.out.println("最大值："+collect1.get());


        Optional<Integer> maxOptional2 = list.stream()
                .collect(Collectors.minBy((o1, o2) -> o1 - o2));
        System.out.println("最小值："+maxOptional2.get());

        Optional<Integer> collect2 = list.stream().min((o1, o2) -> o1 - o2);
        System.out.println("最小值："+collect2.get());


        Integer collect3 = list.stream().collect(Collectors.summingInt(d -> d));
        System.out.println("总数:" + collect3);

        Integer reduce = list.stream().reduce(0, (t, u) -> t + u);
        System.out.println("总数:" + reduce);

        Double collect4 = list.stream().collect(Collectors.averagingInt(d -> d));
        System.out.println("平均数:" + collect4);

        Long collect5 = list.stream().collect(Collectors.counting());
        System.out.println("数量为:" + collect5);
    }
}
